# import sys
# print(sys.version_info)
# sys.version_info(major=3, minor=8, micro=3, releaselevel='final', serial=0)

# 建立字典
temp_2_16_2 = ['0000','0001','0010','0011','0100','0101',
			   '0110','0111','1000','1001','1010','1011',
			   '1100','1101','1110','1111']
temp_2_16_16 = ['0','1','2','3','4','5',
				'6','7','8','9','A','B',
				'C','D','E','F']
temp_2_8_2 = ['000','001','010','011','100','101','110','111']
temp_2_8_8 = ['0','1','2','3','4','5','6','7']


# r 进制转十进制，initial <= 16
# 输入：initial，原始进制格式，x，原始值，可谓字符或数值
# 返回：字符串
def rToDecimalism(initial,x):
	temp = str(x)
	positive = temp.split('.')[0]
	positive = positive[::-1]
	result = 0
	isNegative = 1
	for inx, val in enumerate(positive):
		if val == '-':
			isNegative = -1
		elif val != '0':
			val = temp_2_16_16.index(val.upper())
			result += val * int(initial)**inx

	if len(temp.split('.')) > 1:
		negative = temp.split('.')[1]
		for inx, val in enumerate(negative):
			if val != '0':
				val = temp_2_16_16.index(val.upper())
				result += val / int(initial)**(inx+1)
	return str(result * isNegative)

# 十进制转 r 进制，target <= 10
# 输入：target，目标进制格式，x，原始值，可谓字符或数值
# 返回：字符串
def decimalismToR(target,x):
	temp = float(x)
	isNegative = ''
	if temp < 0:
		isNegative = '-'
		temp *= -1
	result = ''
	positive = float(str(temp).split('.')[0])
	while positive != 0:
		remainder = int(positive % int(target))
		positive = (positive - remainder) / int(target)
		result += temp_2_16_16[remainder]
	result = result[::-1]
	if len(str(temp).split('.')) > 1:
		negative = str(temp).split('.')[1]
		if negative != '0':
			result += '.'
			isOver = -99
			while isOver != 0:
				isOver = float('0.' + negative) * int(target)
				if len(negative) != 0 and isOver != 0.0:
					result += temp_2_16_16[int(isOver)]
					negative = str(isOver)[2:]

	return isNegative + result

# 二进制、八进制、十六进制速算
# 输入：initial，原始进制格式（限制，2、8、16）；target，目标进制格式（限制，2、8、16）；x，原始值，可谓字符或数值；tailComplement，涉及小数时，后补位
# 返回：字符串
def quickCalc(initial,target,x,tailComplement = 0):
	# https://www.sojson.com/hexconvert.html
	# 带小数的，计算还是不正确
	temp = str(x)
	if initial == target:
		return temp

	isNegative = ''
	if temp[0:1] == '-':
		isNegative = '-'
		temp = temp[1:]

	result = ''

	if len(str(temp).split('.')) > 1:
		return isNegative + quickCalc(initial,target,str(temp).split('.')[0]) + '.' + quickCalc(initial,target,str(temp).split('.')[1],1)
	if str(initial) == '2' and str(target) == '8':
		if len(temp) % 3 != 0:
			if tailComplement == 0:
				temp = (3 - len(temp) % 3) * '0' + temp
			else:
				temp += (3 - len(temp) % 3) * '0'
		for i in range(0,int(len(temp) / 3)):
			result += temp_2_8_8[temp_2_8_2.index(temp[i*3:(i+1)*3])]
	elif str(initial) == '2' and str(target) == '16':
		if len(temp) % 4 != 0:
			if tailComplement == 0:
				temp = (4 - len(temp) % 4) * '0' + temp
			else:
				temp += (4 - len(temp) % 4) * '0'
		for i in range(0,int(len(temp) / 4)):
			result += temp_2_16_16[temp_2_16_2.index(temp[i*4:(i+1)*4])]
	elif str(initial) == '8' and str(target) == '2':
		for i in range(0,len(temp)):
			result += temp_2_8_2[temp_2_8_8.index(temp[i])]
			if i == 0 and tailComplement == 0:
				result = result[result.find('1'):]
	elif str(initial) == '16' and str(target) == '2':
		for i in range(0,len(temp)):
			result += temp_2_16_2[temp_2_16_16.index(temp[i])]
			if i == 0 and tailComplement == 0:
				result = result[result.find('1'):]
	elif str(initial) == '8' and str(target) == '16':
		return quickCalc('2','16',quickCalc('8','2',x))
	elif str(initial) == '16' and str(target) == '8':
		return quickCalc('2','8',quickCalc('16','2',x))
	return isNegative + result


print('########################## 2进制转8进制 ##########################')
print("-10100.01，直接转换，" , quickCalc(2,8,-10100.01))
print("-10100.01，经10进制中转，" , decimalismToR(8,rToDecimalism(2,-10100.01)))
print('')

print('########################## 8进制转2进制 ##########################')
print("-24.2，直接转换，" , quickCalc(8,2,-24.2))
print("-24.2，经10进制中转，" , decimalismToR(2,rToDecimalism(8,-24.2)))
print('')

print('########################## 2进制转16进制 ##########################')
print("-10100.01，直接转换，" , quickCalc(2,16,-10100.01))
print("-10100.01，经10进制中转，" , decimalismToR(16,rToDecimalism(2,-10100.01)))
print('')

print('########################## 16进制转2进制 ##########################')
print("-14.4，直接转换，" , quickCalc(16,2,-14.4))
print("-14.4，经10进制中转，" , decimalismToR(2,rToDecimalism(16,-14.4)))
print('')